<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/12/05/0005
 * Time: 8:57
 */

namespace app\sports_admin\controller;


use app\exception\PlanTypeException;
use think\Db;
use think\Request;
use app\sports_admin\model\PlanType as PlanTypeModel;
use app\sports_admin\service\PlayType as PlayTypeService;
class PlanType
{
    protected $beforeActionList = [
        'isResource' => ['only'=>'index,parent,parentinsert,add,edit,insert,update,delete'],
        'operationSelect'=>['only'=>'index,parent,add,edit'],
        'operationInsert'=>['only'=>'parentinsert,insert'],
        'operationUpdate'=>['only'=>'update'],
        'operationDelete'=>['only'=>'delete']
    ];
    //教案类别首页
    public function index()
    {
        $info = PlayTypeService::index();

        return view('index',compact('info'));
    }
    //添加父类页面
    public function parent()
    {
        return view('parent');
    }
    //添加父类
    public function parentInsert(Request $request)
    {
        if($request->isAjax()){
            return PlayTypeService::parentInsert();
        }
    }
    //教案类别添加页面
    public function add()
    {
        $data = Request::instance()->param();
        return view('add',compact('data'));
    }
    /**
     * 添加教案类别
     * @param $title 类别名称
     */
    public function insert(Request $request)
    {
        if($request->isAjax()) {
            return PlayTypeService::insert();
        }
    }
    /**
     * 编辑类别信息页面
     * @param 类别id
     */
    public function edit()
    {
        $id = input('get.id');
        $data = PlanTypeModel::where('id',$id)->find();
        return view('edit',compact('data'));
    }
    /**
     * 添加教案类别
     * @param $title 类别名称
     * @param $id 类别信息id
     */
    public function update(Request $request)
    {
        if($request->isAjax()){
            return PlayTypeService::update();
        }
    }
    /**
     * 编辑父类页面
     * @param  $id 类别id
     */
    public function type()
    {
        $id = intval(input('get.id'));
        $data = PlanTypeModel::where('id',$id)->find();
        $parentInfo = PlanTypeModel::getTypeParentInfo($data->parent_id);
        $typeInfo = PlayTypeService::index();
        return view('type',compact('data','parentInfo','typeInfo'));
    }
    /*
     * 修改类别父类
     */
    public function updateTypeParent(Request $request)
    {
        if($request->isAjax()){
            return PlayTypeService::updateTypeParent();
        }
    }
    /*
     * 类别排序
     *
     */
    public function sort(Request $request)
    {
        if($request->isAjax()){
            return PlayTypeService::sort();
        }
    }
    /**
     * 删除类别
     * @param $id 类别id
    */
    public function delete(Request $request)
    {
        if($request->isAjax()){
            $id = intval($request->post('id'));
            $childType = PlanTypeModel::getTypeChild($id);
            if($childType){
                throw new PlanTypeException([
                    'msg' => '不能删除含有子类的类别',
                    'code' => 202
                ]);
            }
            $bool = PlanTypeModel::destroy($id);
            if(!$bool){
                throw new PlanTypeException([
                    'msg'=>'删除失败',
                    'errorCode'=>400
                ]);
            }
            return json(['code'=>200,'msg'=>'删除成功']);
        }
    }
}